//
//  GTWeChatDynamicListController.m
//  GTW
//
//  Created by Ran on 15/11/12.
//  Copyright (c) 2015年 xcode. All rights reserved.
//

#import "GTWeChatDynamicListController.h"
#import "RanTestModel.h"
#import "GTWeChatDynamicItemModel.h"
#import "GTWeChatDynamicCell.h"

@interface GTWeChatDynamicListController ()<UITableViewDataSource, UITableViewDelegate, GTWeChatDynamicCellDelegate>

@property(nonatomic, strong)UITableView *tableview;
@property(nonatomic, strong)NSArray *originalData;
@property(nonatomic, strong)NSMutableArray *dataSource;

@end

@implementation GTWeChatDynamicListController

#pragma mark - lifeCycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupParams];
    [self setupUI];
    [self.tableview reloadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

#pragma mark - Private

- (void)setupParams
{
    
}

- (void)setupUI
{
    self.view.backgroundColor = [UIColor whiteColor];
}

#pragma mark - Event

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GTWeChatDynamicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.delegate = self;
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

#pragma mark - Delegate

#pragma mark - UITabelViewDelegagte

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.dataSource[indexPath.row] height];
}

#pragma mark - GTWeChatDynamicCellDelegate

- (void)cellHeightChanged:(GTWeChatDynamicCell *)cell
{
//    NSIndexPath *indexPath = [self.tableview indexPathForCell:cell];
//    NSMutableArray *indexPaths = [NSMutableArray array];
//    for (int i = -2; i < 3; i++) {
//        if (indexPath.row + i >=0 && indexPath.row + i < self.dataSource.count) {
//            [indexPaths addObject:[NSIndexPath indexPathForRow:indexPath.row + i inSection:0]];
//        }
//    }
//    [self.tableview reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
    [self.tableview reloadData];
}

#pragma mark - Getter 

- (UITableView *)tableview
{
    if (!_tableview) {
        self.tableview = ({
            UITableView *tableview = [[UITableView alloc] initWithFrame:self.view.bounds];
            tableview.delegate = self;
            tableview.dataSource = self;
            tableview.tableFooterView = [[UIView alloc] init];
//            tableview.delaysContentTouches = NO;
            [self.view addSubview:tableview];
            [tableview registerClass:[GTWeChatDynamicCell class] forCellReuseIdentifier:@"cell"];
            tableview;
        });
    }
    return _tableview;
}

- (id)random:(NSArray *)arr
{
    return arr[arc4random_uniform(arr.count)];
}

- (NSString *)randomContent
{
    NSString *baseString = @"我的未来不是梦";
    NSMutableString *str = [NSMutableString stringWithString:baseString];
    int num = arc4random_uniform(50);
    for ( int i = 0; i < num; i++) {
        [str appendString:baseString];
    }
    return str;
}

- (NSArray *)randomAts
{
    NSMutableArray *arrM = [NSMutableArray arrayWithObjects:@{@"name": @"Rose"}, nil];
    NSArray *atOnes = @[
               @{@"name": @"Jack"},
               @{@"name": @"王大锤"},
               @{@"name": @"叫兽易小星"},
               @{@"name": @"子墨布拉德皮蛋"},
               @{@"name": @"老湿alwayswet"},
               @{@"name": @"出米老"},
               @{@"name": @"要你命三千"},
               @{@"name": @"神雕侠侣"},
               @{@"name": @"郭玉芬的闺蜜"},
               @{@"name": @"他的为"},
               @{@"name": @"吸毒"},
               @{@"name": @"饭桶张"}
               ];
    int num = arc4random_uniform(atOnes.count);
    for (int i = 0; i < num; i++) {
        [arrM addObject:atOnes[i]];
    }
    return arrM;
}

- (NSArray *)randomLinks
{
    NSMutableArray *arrM = [NSMutableArray arrayWithObjects:@{@"name": @"人脉(3)"}, nil];
    NSArray *atOnes = @[
                        @{@"name": @"组织(2)"},
                        @{@"name": @"知识(32)"},
                        @{@"name": @"事件(11)"},
                        ];
    int num = arc4random_uniform(atOnes.count);
    for (int i = 0; i < num; i++) {
        [arrM addObject:atOnes[i]];
    }
    return arrM;
}

- (NSArray *)randomTags
{
    NSMutableArray *arrM = [NSMutableArray arrayWithObjects:@{@"name": @"融资"}, nil];
    NSArray *atOnes = @[
                        @{@"name": @"军事向"},
                        @{@"name": @"局势"},
                        @{@"name": @"经济发展"},
                        @{@"name": @"金融"},
                        @{@"name": @"军事向"},
                        @{@"name": @"局势"},
                        @{@"name": @"经济发展"},
                        @{@"name": @"金融"},
                        ];
    int num = arc4random_uniform(atOnes.count);
    for (int i = 0; i < num; i++) {
        [arrM addObject:atOnes[i]];
    }
    return arrM;
}

- (NSArray *)originalData
{
    if (!_originalData) {
        self.originalData = ({
            NSArray *names = @[@"C", @"葫芦", @"小当家", @"布拉德皮特", @"塔木贾诺维奇", @"拉克拉嘎干"];
            NSArray *imgs = @[@"1.jpg", @"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",];
            NSArray *times = @[@"刚刚", @"5分钟前", @"3天前", @"一个月前", @"9月24日", @"2013年8月10日"];
            NSArray *locations = @[@"北京 西三旗桥东公交站", @"大连 北道沟单立屯体育中I型你俱乐部", @"外面",  @"黑龙江省双城市新形象新生村十字街10号左转二楼409房间第一把椅子",];
            NSMutableArray *arrayM = [NSMutableArray array];
            for (int i = 0; i < 30; i++) {
                RanTestModel *model = [[RanTestModel alloc] init];
                model.imageUrl = [self random:imgs];
                model.name = [self random:names];
                model.timeStamp = [self random:times];
                model.location = [self random:locations];
                model.content = [self randomContent];
                model.atOnes = [self randomAts];
                model.linkOnes = [self randomLinks];
                model.tagOnes = [self randomTags];
                [arrayM addObject:model];
            }
            arrayM;
        });
    }
    return _originalData;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        self.dataSource = ({
            NSMutableArray *arrayM = [NSMutableArray array];
            for (RanTestModel *sourceData in self.originalData) {
                GTWeChatDynamicItemModel *model = [[GTWeChatDynamicItemModel alloc] init];
                model.sourceData = sourceData;
                [arrayM addObject:model];
            }
            arrayM;
        });
    }
    return _dataSource;
}

@end
